package cn.itcast.hiss.server.order;

import cn.itcast.hiss.server.mapper.GoodsMapper;
import cn.itcast.hiss.server.util.SpringContextHolder;
import org.activiti.api.runtime.model.impl.BPMNErrorImpl;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/*
 * @author miukoo
 * @description 创建采购清单+仓库入库
 * @date 2023/8/23 18:53
 * @version 1.0
 **/
public class BuyServiceTask implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        GoodsMapper goodsMapper = applicationContext.getBean(GoodsMapper.class);
        List<Map<String, Object>> goodList = goodsMapper.listGoods();
        // ==============================  生成采购清单 ================
        // 采购清单
        StringBuilder list = new StringBuilder();
        for (Map<String, Object> good : goodList) {
            int sales = Integer.valueOf(good.get("sales") + "");
            sales = (int) (sales * 0.3);
            String temp = String.format("<ul><li>商品：%s</li><li>采购数量：%s</li><li>采购商家：%s</li></ul>",good.get("title"),sales,good.get("purchaser"));
            list.append(temp);
        }
        // 设置需要采购的清单
        execution.setVariable("buyContent",list);

        // ==============================  仓库入库  ================
        StringJoiner store = new StringJoiner(",");
        for (Map<String, Object> good : goodList) {
            int sales = Integer.valueOf(good.get("sales") + "");
            sales = (int) (sales * 0.3);
            store.add(good.get("id")+"#"+sales);
        }
        // 设置入库商品的库存
        execution.setVariable("storeContent",store.toString());
    }
}
